package com.my.utils;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * package:
 * author:
 * date:
 */
@Data
public class RedisUtils {

    public RedisUtils(){

    }

    @Autowired
    RedisTemplate redisTemplate;

    @Bean
    public  <T> T getObject(String key, Class<T> clazz) {
        if (null == redisTemplate.opsForValue().get(key)){
            return null;
        }
        Object valueObj = redisTemplate.opsForValue().get(key);
        if (clazz.isInstance(valueObj)) {
            return (T) valueObj;
        } else if (clazz == Long.class && valueObj instanceof Integer) {
            Integer obj = (Integer) valueObj;
            return (T) Long.valueOf(obj.longValue());
        }
        return null;
    }
}
